<?php 
	jimport('joomla.application.component.model');
	class kinh_doanhModelsu_luoc_ban_hang extends JModel
	{
        //danh sach ban hang trong ngay	   
		function su_luoc_ban_hang($date=null)   
		{
            //neu date = null thi lay ngay hien hanh
            if($date == null)
            {
                $date = date('Y-m-d');
            }
            //dinh dang lai $date giong voi CSDL
            else
            {
                $d = substr($date,3,2);
                $m = substr($date,0,2);
                $y = substr($date,6,4);
                $date = $y."-".$m."-".$d;
                
            }
            $user = JFactory::getUser();
            //dang nhap thanh cong lay duoc id -> lay id_a_b tu table bran_user, 
            $sql = "select id_a_b from #__erp_agent_branch_user where id_user ='".$user->get('id')."' ";
            $db = &JFactory::getDBO();
            $db->setQuery($sql);
            $id_a_b = $db->loadResult();
            //lay danh sach san hang theo ngay 
            $_SESSION['id_a_b'] = $id_a_b; 	
            $sql = "    select  #__erp_cuppon.code_cuppon, 
                             #__erp_agent_branch_sale.number_of,
                             #__erp_agent_branch_sale.total, 
                             #__erp_product_custom_profile.id_user as id_customer, 
                             #__erp_financial_currency.symbol as currency,
                             #__erp_product.name as name_product,
                             #__erp_product.id_product as id_product,
                             #__erp_product.id_type_product as id_type_product,
                             #__erp_agent_branch_sale.note,
                             #__erp_agent_branch_sale.date,
                              #__erp_agent_branch_sale.id
                        FROM #__erp_agent_branch_sale
                        LEFT JOIN #__erp_cuppon
                        ON #__erp_agent_branch_sale.id_cuppon = #__erp_cuppon.id
                        LEFT JOIN #__erp_product
                        ON #__erp_agent_branch_sale.id_product = #__erp_product.id_product
                        LEFT JOIN #__erp_product_custom_profile
                        ON #__erp_agent_branch_sale.id_customer = #__erp_product_custom_profile.id_user
                        LEFT JOIN #__erp_financial_currency
                        ON #__erp_agent_branch_sale.id_currency = #__erp_financial_currency.id
                        where #__erp_agent_branch_sale.id_a_b = '".$_SESSION['id_a_b']."'
                        AND #__erp_agent_branch_sale.date LIKE '$date%' 
                        AND #__erp_agent_branch_sale.deleted = 0
                        
                    ";//chua de dk where vao
                    //where #__erp_agent_branch_sale.date= '2012-10-28'
                        //and #__erp_agent_branch_sale.id_a_b = '3'
                    $db->setQuery($sql);
                    //print_r($db->loadObjectList()); die;
                    return $db->loadObjectList();
                   
            
                    
                        
                        
		  /*
                    //lay id user
                    $user = JFactory::getUser();
                    //dang nhap thanh cong lay duoc id -> lay id_a_b tu table bran_user, 
                    $sql = "select id_a_b from #__erp_agent_branch_user where id_user ='".$user->get('id')."' ";
                    $db = &JFactory::getDBO();
        		    $db->setQuery($sql);
        		    $id_a_b = $db->loadResult();
                    $_SESSION['id_a_b'] = $id_a_b; 	
                    //lay danh sach san pham trong kho thuoc chi nhanh thong qua id_a_b
                    $sql = "select ep.id_product,ep.name, ep.img, ept.id as id_type, ept.name as name_type, eabw.number_of, ep.price, cur.name as cur_name, cur.id as cur_id, cur.symbol, ui.name as ui_name, eab.id as id_a_b
                            from #__erp_product ep ,#__erp_product_type ept ,#__erp_agent_branch eab,#__erp_agent_branch_warehouse eabw ,  #__erp_financial_currency cur, #__erp_product_unit ui
                            where 
                            ep.id_type_product = ept.id and 
                            ep.id_product = eabw.id_product and
                            eab.id = eabw.id_a_b and
                            ep.id_currency = cur.id and
                            ui.id = ep.id_unit and
                            eab.id = '".$id_a_b."' 
                            order by ept.name     
                        ";
                        //echo $sql;
			$db = &JFactory::getDBO();
			$db->setQuery($sql);
			return $db->loadObjectList();	*/
            
		}
        
        //tong so tien tu truoc den gio tai cua hang nay
        function tong_tien_truoc_den_gio()
        {
            $sql = "select sum(total) from #__erp_agent_branch_sale where id_a_b= '".$_SESSION['id_a_b']."'";
            $db = &JFactory::getDBO();
            $db->setQuery($sql);
            $sum_truoc_den_gio = $db->loadResult();
            return $sum_truoc_den_gio;
        }
        
        //tong so ban trong 1 ngay
        function tong_tien_trong_ngay()
        {
            $date = date('Y-m-d');
            $sql = "select sum(total) from #__erp_agent_branch_sale where id_a_b= '".$_SESSION['id_a_b']."'
            AND date LIKE '$date%' AND deleted = 0
            ";
            $db = &JFactory::getDBO();
            $db->setQuery($sql);
            $rs= $db->loadResult();
            return $rs;
        }
        
        //So tien hien co tai chi nhanh
        function tien_tai_chi_nhanh()
        {
            $sql = "select balance from #__erp_agent_branch_account where id_a_b= '".$_SESSION['id_a_b']."'";
            $db = &JFactory::getDBO();
            $db->setQuery($sql);
            $rs= $db->loadResult();
            return $rs;
        
        }
	}
?>